package fmod.org.smali.myapplication.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.animation.DecelerateInterpolator;
import android.widget.Scroller;
import android.widget.ViewFlipper;

public class PlayerDiscViewFlipper extends ViewFlipper {
    private GestureDetector gestureDetector;
    private Scroller scroller;
    private OnClickListener singleClickListener;
    private OnLongClickListener longClickListener;
    private FlipperListener fliperListener;
    private float deltaX;
    private int startPosX;
    private int displayedChildIndex;
    private boolean isSingleClick;
    private boolean isScrolling;
    private boolean isStartScrollLeft;
    private boolean isStartScrollRight;
    private boolean isScrollToRight;
    private boolean isAutoScrollMode;
    private boolean isFirstScrollRightOverCenter;
    private boolean isFirstScrollLeftOverCenter;
    private boolean isNotScrollOverCener;
    private boolean isFinished;
    private boolean enableScroll;
    private long startScrollTime;
    private int scrollCount;
    private OnGestureListener gestureDetectorListener;

    public interface FlipperListener {

        void onScrollFinished(boolean hasDisplayNextView, boolean isAutoScrollMode, boolean isScrollToRight);

        void onStartScroll(Boolean aBoolean);

        void onScrollOverCener(Boolean aBoolean);

        void m5742a(boolean b);
    }

    class RequestLayoutRunnable implements Runnable {
        final /* synthetic */ PlayerDiscViewFlipper viewFlipper;

        RequestLayoutRunnable(PlayerDiscViewFlipper playerDiscViewFlipper) {
            this.viewFlipper = playerDiscViewFlipper;
        }

        public void run() {
            this.viewFlipper.requestLayout();
        }
    }

    class ScrollFinishRunnable implements Runnable {
        final /* synthetic */ boolean hasDisplayNextView;
        final /* synthetic */ PlayerDiscViewFlipper viewFlipper;

        ScrollFinishRunnable(PlayerDiscViewFlipper playerDiscViewFlipper, boolean hasDisplayNextView) {
            this.viewFlipper = playerDiscViewFlipper;
            this.hasDisplayNextView = hasDisplayNextView;
        }

        public void run() {
            this.viewFlipper.fliperListener.onScrollFinished(this.hasDisplayNextView, this.viewFlipper.isAutoScrollMode, this.viewFlipper.isScrollToRight);
        }
    }

    class GestureListener extends SimpleOnGestureListener {
        final /* synthetic */ PlayerDiscViewFlipper playerDiscViewFlipper;

        GestureListener(PlayerDiscViewFlipper playerDiscViewFlipper) {
            this.playerDiscViewFlipper = playerDiscViewFlipper;
        }

        public boolean onScroll(MotionEvent downMotionEvent, MotionEvent motionEvent, float distanceX, float distanceY) {
            if (downMotionEvent == null || motionEvent == null) {
                return false;
            }
            playerDiscViewFlipper.deltaX = downMotionEvent.getX() - motionEvent.getX();
            playerDiscViewFlipper.isScrollToRight = playerDiscViewFlipper.deltaX > 0.0f;
            if (!playerDiscViewFlipper.isScrolling) {
                playerDiscViewFlipper.isScrolling = true;
                playerDiscViewFlipper.isAutoScrollMode = false;
                if (playerDiscViewFlipper.fliperListener != null) {
                    boolean isScrollToRight;
                    FlipperListener fliperListener = playerDiscViewFlipper.fliperListener;
                    if (playerDiscViewFlipper.deltaX > 0.0f) {
                        isScrollToRight = true;
                    } else {
                        isScrollToRight = false;
                    }
                    fliperListener.onStartScroll(isScrollToRight);

                }
            }
            if (playerDiscViewFlipper.getCurrentView().getLeft() < 0 && !playerDiscViewFlipper.isStartScrollLeft) {
                if (playerDiscViewFlipper.fliperListener != null) {
                    playerDiscViewFlipper.fliperListener.onStartScroll(Boolean.valueOf(false));
                }
                playerDiscViewFlipper.isStartScrollRight = false;
                playerDiscViewFlipper.isStartScrollLeft = true;
            } else if (playerDiscViewFlipper.getCurrentView().getLeft() > 0 && !playerDiscViewFlipper.isStartScrollRight) {
                if (playerDiscViewFlipper.fliperListener != null) {
                    playerDiscViewFlipper.fliperListener.onStartScroll(Boolean.valueOf(true));
                }
                playerDiscViewFlipper.isStartScrollRight = true;
                playerDiscViewFlipper.isStartScrollLeft = false;
            }
            if (playerDiscViewFlipper.getCurrentView().getLeft() < (-playerDiscViewFlipper.getWidth()) / 2 && !playerDiscViewFlipper.isFirstScrollLeftOverCenter) {
                if (playerDiscViewFlipper.fliperListener != null) {
                    playerDiscViewFlipper.fliperListener.onScrollOverCener(Boolean.valueOf(false));
                }
                playerDiscViewFlipper.isNotScrollOverCener = false;
                playerDiscViewFlipper.isFirstScrollRightOverCenter = false;
                playerDiscViewFlipper.isFirstScrollLeftOverCenter = true;
            } else if (playerDiscViewFlipper.getCurrentView().getLeft() > playerDiscViewFlipper.getWidth() / 2 && !playerDiscViewFlipper.isFirstScrollRightOverCenter) {
                if (playerDiscViewFlipper.fliperListener != null) {
                    playerDiscViewFlipper.fliperListener.onScrollOverCener(Boolean.valueOf(true));
                }
                playerDiscViewFlipper.isNotScrollOverCener = false;
                playerDiscViewFlipper.isFirstScrollRightOverCenter = true;
                playerDiscViewFlipper.isFirstScrollLeftOverCenter = false;
            } else if (((playerDiscViewFlipper.getCurrentView().getLeft() >= (-playerDiscViewFlipper.getWidth()) / 2 
                    && playerDiscViewFlipper.getCurrentView().getLeft() < 0) 
                    || (playerDiscViewFlipper.getCurrentView().getLeft() <= playerDiscViewFlipper.getWidth() / 2 
                    && playerDiscViewFlipper.getCurrentView().getLeft() > 0)) 
                    && !playerDiscViewFlipper.isNotScrollOverCener) {
                if (playerDiscViewFlipper.fliperListener != null) {
                    playerDiscViewFlipper.fliperListener.onScrollOverCener(null);
                }
                playerDiscViewFlipper.isNotScrollOverCener = true;
                playerDiscViewFlipper.isFirstScrollRightOverCenter = false;
                playerDiscViewFlipper.isFirstScrollLeftOverCenter = false;
            }
            playerDiscViewFlipper.requestLayout();
            return true;
        }

        public boolean onDown(MotionEvent motionEvent) {
            playerDiscViewFlipper.startPosX = playerDiscViewFlipper.getCurrentView().getLeft();
            playerDiscViewFlipper.deltaX = 0.0f;
            playerDiscViewFlipper.isAutoScrollMode = false;
            return true;
        }

        public boolean onSingleTapUp(MotionEvent motionEvent) {
            if (playerDiscViewFlipper.singleClickListener != null) {
                playerDiscViewFlipper.singleClickListener.onClick(playerDiscViewFlipper);
            }
            return true;
        }

        public void onLongPress(MotionEvent motionEvent) {
            if (playerDiscViewFlipper.longClickListener != null) {
                playerDiscViewFlipper.longClickListener.onLongClick(playerDiscViewFlipper);
            }
        }
    }

    public PlayerDiscViewFlipper(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.deltaX = 0.0f;
        this.startPosX = 0;
        this.displayedChildIndex = -1;
        this.isSingleClick = false;
        this.isScrolling = false;
        this.enableScroll = true;
        this.startScrollTime = 0;
        this.scrollCount = 0;
        this.gestureDetectorListener = new GestureListener(this);
        this.gestureDetector = new GestureDetector(getContext(), this.gestureDetectorListener);
        this.scroller = new Scroller(getContext(), new DecelerateInterpolator());
    }

    public void setGestureEnable(boolean isEnable) {
        this.enableScroll = isEnable;
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.singleClickListener = onClickListener;
    }

    public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
        this.longClickListener = onLongClickListener;
    }

    public void setOnPlayerDiscListener(FlipperListener fliperListener) {
        this.fliperListener = fliperListener;
    }

    public boolean isSingleClick() {
        return this.isSingleClick;
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (!this.enableScroll) {
            return false;
        }
        if (motionEvent.getAction() == 0) {
            this.isSingleClick = true;
            this.startScrollTime = System.currentTimeMillis();
        } else if (motionEvent.getAction() == 1 || motionEvent.getAction() == 3) {
            if (this.deltaX != 0.0f || this.isAutoScrollMode) {
                this.scrollCount++;
            }
            this.isSingleClick = false;
            this.isNotScrollOverCener = false;
            this.isFirstScrollRightOverCenter = false;
            this.isFirstScrollLeftOverCenter = false;
            this.isStartScrollLeft = false;
            this.isStartScrollRight = false;
            this.isScrolling = false;
            float currentTimeMillis = this.deltaX / ((float) (System.currentTimeMillis() - this.startScrollTime));
            this.deltaX = 0.0f;
            int left = getCurrentView().getLeft();
            if (left > getMeasuredWidth() / 2 || ((double) currentTimeMillis) < -1.7d) {
                this.displayedChildIndex = getDisplayedChild() + 1;
                if (this.isFinished) {
                    this.scroller.startScroll(left, 0, getMeasuredWidth() - getCurrentView().getLeft(), 0, 400);
                }
            } else if (left > 0) {
                if (this.isFinished) {
                    this.scroller.startScroll(left, 0, -getCurrentView().getLeft(), 0, 400);
                }
            } else if (left < (-getMeasuredWidth()) / 2 || ((double) currentTimeMillis) > 1.7d) {
                this.displayedChildIndex = getDisplayedChild() + 1;
                if (this.isFinished) {
                    this.scroller.startScroll(left, 0, (-getMeasuredWidth()) - left, 0, 400);
                }
            } else if (left < 0 && this.isFinished) {
                this.scroller.startScroll(left, 0, -left, 0, 400);
            }
            requestLayout();
        }
        boolean isFinished = this.scroller.isFinished();
        if (motionEvent.getAction() == 0) {
            this.isFinished = isFinished;
        }
        if (isFinished) {
            if (!this.isFinished) {
                this.isFinished = true;
                MotionEvent obtain = MotionEvent.obtain(motionEvent);
                obtain.setAction(0);
                this.gestureDetector.onTouchEvent(obtain);
            }
            this.gestureDetector.onTouchEvent(motionEvent);
        }
        return true;
    }

    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        int currX;
        boolean hasDisplayNextView = false;
        if (this.scroller.computeScrollOffset()) {
            currX = this.scroller.getCurrX();
        } else {
            currX = (int) (((float) this.startPosX) - this.deltaX);
        }
        if (currX <= (-getMeasuredWidth()) && this.isScrollToRight) {
            currX = -getMeasuredWidth();
        }
        int measuredWidth = currX + getMeasuredWidth();
        getCurrentView().layout(currX, 0, measuredWidth, getMeasuredHeight());
        int measuredWidth2 = getMeasuredWidth() + measuredWidth;
        if (this.isScrollToRight) {
            currX = measuredWidth2;
            measuredWidth2 = measuredWidth;
        } else {
            currX -= getMeasuredWidth();
            if (currX > 0) {
                currX = 0;
            }
            measuredWidth2 = currX;
            currX = getMeasuredWidth() + currX;
        }
        if (measuredWidth2 < getMeasuredWidth() || measuredWidth2 > 0) {
            View nextView = getNextView();
            nextView.setVisibility(View.VISIBLE);
            nextView.layout(measuredWidth2, 0, currX, getMeasuredHeight());
        }
        if (this.scroller.isFinished()) {
            if (this.displayedChildIndex != -1) {
                setDisplayedChild(this.displayedChildIndex);
                this.displayedChildIndex = -1;
            } else {
                hasDisplayNextView = true;
            }
            if (this.scrollCount > 0) {
                if (this.fliperListener != null) {
                    post(new ScrollFinishRunnable(this, hasDisplayNextView));
                }
                this.scrollCount--;
                return;
            }
            return;
        }
        post(new RequestLayoutRunnable(this));
    }

    public void smoothScrollTo(boolean right) {
        int i = 1;
        if (!this.scroller.isFinished()) {
            this.scroller.forceFinished(true);
            if (this.scrollCount > 0) {
                if (this.fliperListener != null) {
                    this.fliperListener.onScrollFinished(false, true, right);
                }
                this.scrollCount--;
            }
        }
        if (this.fliperListener != null) {
            this.fliperListener.m5742a(this.deltaX > 0.0f);
        }
        if (this.fliperListener != null) {
            this.fliperListener.onStartScroll(null);
        }
        this.isScrollToRight = right;
        this.isAutoScrollMode = true;
        this.displayedChildIndex = getDisplayedChild() + 1;
        this.scrollCount++;
        Scroller scroller = this.scroller;
        int width = getWidth();
        if (right) {
            i = -1;
        }
        scroller.startScroll(0, 0, width * i, 0, 400);
        requestLayout();
    }

    public View getNextView() {
        int i = 1;
        if (getChildAt(1) == getCurrentView()) {
            i = 0;
        }
        return getChildAt(i);
    }
}
